import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import enums.Shape;


public class ToolBox extends JPanel{
	
	private JPanel toolPanel;
	private DrawPad drawPad;
	
	public ToolBox(DrawPad pad){
		
		drawPad = pad;
		toolPanel = new JPanel();
		
		GroupLayout layout = new GroupLayout(toolPanel);
		toolPanel.setLayout(layout);
		
		//layout.setAutoCreateGaps(true);
		//layout.setAutoCreateContainerGaps(true);
		
		//Button set
		JButton clear = this.setClearButton();
		JButton getColor = this.setGetColorButton();
		JButton pencil = this.setBrushButton();
		JButton drawLine = this.setDrawLineButton();
		JButton drawRectangle = this.setDrawRectangleButton();
		JButton drawOval = this.setDrawOvalButton();
		JButton fillRectangle = this.setFillRectangleButton();
		JButton fillOval = this.setFillOvalButton();
		JButton fillArea = this.setFillAreaButton();
		
		// Horizontal Group
		layout.setHorizontalGroup(layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
						//.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 3, 3)
						.addComponent(clear, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
						//.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 3, 3)
						.addComponent(getColor, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
				)
				.addGroup(layout.createSequentialGroup()
						//.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 3, 3)
						.addComponent(pencil, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
						//.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 3, 3)
						.addComponent(drawLine, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
				)
				.addGroup(layout.createSequentialGroup()
						//.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 3, 3)
						.addComponent(drawRectangle, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
						//.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 3, 3)
						.addComponent(fillRectangle, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
				)
				.addGroup(layout.createSequentialGroup()
						//.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 3, 3)
						.addComponent(drawOval, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
						.addComponent(fillOval, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
				)
				
		);		
		// Vertical Group
		layout.setVerticalGroup(layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup()
						.addComponent(clear, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
						.addComponent(getColor, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
				)
				.addGroup(layout.createParallelGroup()
						.addComponent(pencil, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
						.addComponent(drawLine, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
				)
				.addGroup(layout.createParallelGroup()
						.addComponent(drawRectangle, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
						.addComponent(fillRectangle, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
				)
				.addGroup(layout.createParallelGroup()
						.addComponent(drawOval, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
						.addComponent(fillOval, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
				)
				
		);
	}
	
	public JPanel getToolPanel()
	{
		return toolPanel;
	}
	
	//Brush button
	private JButton setBrushButton(){
		
		JButton pencilButton = new JButton(new ImageIcon("images/tools/brush.png"));
		pencilButton.setBorderPainted(false);
		pencilButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				drawPad.setShape(Shape.BRUSH);
			}
		});
		return pencilButton;
	}
	//Draw Line Button
	private JButton setDrawLineButton(){
		
		JButton drawLineButton = new JButton(new ImageIcon("images/tools/line.png"));
		drawLineButton.setBorderPainted(false);
		drawLineButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				drawPad.setShape(Shape.LINE);
			}
		});
		return drawLineButton;
	}
	
	//Draw rectangle button
	private JButton setDrawRectangleButton(){
		
		JButton drawRectangleButton = new JButton(new ImageIcon("images/tools/drawRect.png"));
		drawRectangleButton.setBorderPainted(false);
		drawRectangleButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				drawPad.setShape(Shape.RECTANGLE);
			}
		});
		return drawRectangleButton;
	}
	
	private JButton setFillRectangleButton(){
		
		JButton fillRectangleButton = new JButton(new ImageIcon("images/tools/fillRect.png"));
		fillRectangleButton.setBorderPainted(false);
		fillRectangleButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				drawPad.setShape(Shape.FILLED_RECTANGLE);
			}
		});
		return fillRectangleButton;
	}
	
	//Draw rectangle button
	private JButton setDrawOvalButton(){
		
		JButton drawOvalButton = new JButton(new ImageIcon("images/tools/drawOval.png"));
		drawOvalButton.setBorderPainted(false);
		drawOvalButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				drawPad.setShape(Shape.OVAL);
			}
		});
		return drawOvalButton;
	}
	
	//Draw rectangle button
	private JButton setFillOvalButton(){
		
		JButton fillOvalButton = new JButton(new ImageIcon("images/tools/fillOval.png"));
		fillOvalButton.setBorderPainted(false);
		fillOvalButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				drawPad.setShape(Shape.FILLED_OVAL);
			}
		});
		return fillOvalButton;
	}
	
	//Fill area Button
	private JButton setFillAreaButton(){
		
		JButton fillAreaButton = new JButton(new ImageIcon("images/tools/fill.png"));
		fillAreaButton.setBorderPainted(false);
		fillAreaButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				drawPad.setShape(Shape.FILL);
			}
		});
		return fillAreaButton;
	}
	
	//Clear button
	private JButton setClearButton(){
		
		JButton clearButton = new JButton(new ImageIcon("images/tools/eraser.png"));
		clearButton.setBorderPainted(false);
		clearButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				drawPad.setShape(Shape.ERASER);
			}
		});
		return clearButton;
	}
	
	//Color pallete button
	private JButton setGetColorButton(){
		
		JButton getColorButton = new JButton(new ImageIcon("images/tools/pallete.png"));
		getColorButton.setBorderPainted(false);
		getColorButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				drawPad.getColor();
			}
		});
		return getColorButton;
	}

}
